/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.unitn.science.matr155863_148346_145963.bccstore.items;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import it.unitn.science.matr155863_148346_145963.bccstore.items.Commission;
import java.awt.Color;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.util.Date;
import java.util.Random;

/**
 *
 * @author Luca
 */
public class CreatePDF {

    public static final float bollo=0.99f;
    public static final float ritenutaFisc=0.01f;

    /**
     * @param args the command line arguments
     */
    public static void createPDF(Commission commission,String path) throws DocumentException, FileNotFoundException, BadElementException, MalformedURLException, IOException {
        float prezzoTot=0f;
        Document document = new Document();
        // step 2:
        // we create a writer that listens to the document
        // and directs a PDF-stream to a file

        //File bill=new File(path+"/bill/"+commission.getBill());
        //System.out.println(bill.getAbsolutePath());
        //I file vengono salvati nella directory:
        // BCCStore/build/web/bill/
        FileOutputStream out= new FileOutputStream(path+"/bill/"+commission.getBill());
        PdfWriter.getInstance(document,out );
        // step 3: we open the document
        document.open();

        //immagine logo sito
        Image png = Image.getInstance(path+"/images/themplate/logo_small.png");
        document.add(png); //formato
        //descrizione in alto a sx dati venditore
        Paragraph p1 = new Paragraph(new Chunk( "BCC Store", FontFactory.getFont(FontFactory.HELVETICA, 20)));
        Paragraph bill = new Paragraph("IdBill: "+commission.getId(),FontFactory.getFont(FontFactory.HELVETICA, 10));
        Paragraph buyer= new Paragraph("Buyer: \n"
                +"first name : "+commission.getBuyerFirstName()+"\n"
                +"last name: "  +commission.getBuyerLastName()+"\n"
                +"username: "   +commission.getBuyerUserName()
                ,FontFactory.getFont(FontFactory.HELVETICA, 10)
                );
        Paragraph seller = new Paragraph("Seller: \n"
                +"first name: "+commission.getSellerFirstName()+"\n"
                +"last name: " +commission.getSellerLastName()+"\n"
                +"username: "  +commission.getBuyerUserName()
                ,FontFactory.getFont(FontFactory.HELVETICA, 10)
                );
        

        //Data e Ora in tipologia Stringa
        Paragraph data = new Paragraph ("Date: "+commission.getDate().toString(), FontFactory.getFont(FontFactory.HELVETICA, 10));

        //Codice casuale
        p1.add(bill); 
        p1.add(buyer);
        p1.add(seller);
        p1.add(data);   
        p1.add(new Paragraph(" "));
        p1.setAlignment(Element.ALIGN_JUSTIFIED);
        p1.setIndentationLeft(1f);
        p1.setIndentationRight(50f);
        document.add(p1);
        //immagine dx basso
        Image jpg = Image.getInstance(path+"/images/products/"+commission.getProduct().getImageName());
        //formato immagine
        
        //da mettere al posto della scritta venditore

        Paragraph p2 = new Paragraph(new Chunk( "",
        FontFactory.getFont(FontFactory.HELVETICA, 10)));

        p2.setAlignment(Element.ALIGN_JUSTIFIED);
        p2.setIndentationLeft(125f);
        p2.setIndentationRight((5 - 1) * 1f);
        p2.add(jpg); //aggiungo l'immagine
        p2.add(new Paragraph(" "));

        document.add(p2);


        //Tabella      
        PdfPTable table=new PdfPTable(2);
        PdfPCell cell = new PdfPCell (new Paragraph ("Descrizione"));
        cell.setHorizontalAlignment (Element.ALIGN_CENTER);
        cell.setPadding (10.0f);  
        table.addCell (cell);

        cell = new PdfPCell (new Paragraph ("Prezzo"));
        cell.setHorizontalAlignment (Element.ALIGN_CENTER);
        table.addCell (cell);

        float[] columnWidths = {2.5f, 0.5f,};
        table.setWidths(columnWidths);

        //prezzo prima riga
        PdfPCell prodotto = new PdfPCell(new Paragraph(commission.getProduct().getName()));
        PdfPCell prezzo_prodotto = new PdfPCell(new Paragraph(""+commission.getProduct().getPrice()));
        prezzo_prodotto.setHorizontalAlignment(Element.ALIGN_CENTER);
        //unità misura
        PdfPCell misura = new PdfPCell(new Paragraph("Unità misura"));
        PdfPCell misura_prodotto = new PdfPCell(new Paragraph(commission.getUmName()));
        misura_prodotto.setHorizontalAlignment(Element.ALIGN_CENTER);
        //quantità
        PdfPCell quantita = new PdfPCell(new Paragraph("Quantità"));
        PdfPCell quantita_prodotto = new PdfPCell(new Paragraph("x "+commission.getQuantity()));
        quantita_prodotto.setHorizontalAlignment(Element.ALIGN_CENTER);
        //riga vuota
        PdfPCell cellvuota = new PdfPCell(new Paragraph(" "));
        cellvuota.setColspan(2);
        //riga vuota2
        PdfPCell cellvuota2 = new PdfPCell(new Paragraph(" "));
        cellvuota2.setColspan(2);
        //celle finali (quelle in basso della tabella)
        PdfPCell cellprezzomerce = new PdfPCell(new Paragraph("Prezzo merce"));
        cellprezzomerce.setHorizontalAlignment(Element.ALIGN_RIGHT);
        PdfPCell cellmercepz = new PdfPCell(new Paragraph(""+commission.getPrice()*commission.getQuantity()));
        cellmercepz.setHorizontalAlignment(Element.ALIGN_CENTER);
        
        PdfPCell cellbollo = new PdfPCell(new Paragraph("Bollo"));
        cellbollo.setHorizontalAlignment(Element.ALIGN_RIGHT);
        PdfPCell cellbollopz = new PdfPCell(new Paragraph(""+bollo));
        cellbollopz.setHorizontalAlignment(Element.ALIGN_CENTER);

        PdfPCell cellritenuta = new PdfPCell(new Paragraph("Ritenuta fiscale"));
        cellritenuta.setHorizontalAlignment(Element.ALIGN_RIGHT);
        PdfPCell cellritenutapz = new PdfPCell(new Paragraph(""+ritenutaFisc));
        cellritenutapz.setHorizontalAlignment(Element.ALIGN_CENTER);

        prezzoTot=commission.getPrice()*commission.getQuantity()+bollo+ritenutaFisc;
        prezzoTot=(float) (Math.round(prezzoTot*100.0)/100.0);
        PdfPCell cellprezzotot = new PdfPCell(new Paragraph("Prezzo totale"));
        cellprezzotot.setHorizontalAlignment(Element.ALIGN_RIGHT);
        PdfPCell celltotpz = new PdfPCell(new Paragraph(""+prezzoTot));
        celltotpz.setHorizontalAlignment(Element.ALIGN_CENTER);


        table.addCell(prodotto);
        table.addCell(prezzo_prodotto);
        table.addCell(misura);
        table.addCell(misura_prodotto);
        table.addCell(quantita);
        table.addCell(quantita_prodotto);
        
        //table.addCell(cellbigvuotadx);
        table.addCell(cellvuota);
        table.addCell(cellvuota2);
  
        table.addCell(cellprezzomerce);
        table.addCell(cellmercepz);
        table.addCell(cellbollo);
        table.addCell(cellbollopz);
        table.addCell(cellritenuta);
        table.addCell(cellritenutapz);
        table.addCell(cellprezzotot);
        table.addCell(celltotpz);

        document.add(table);
        // step 5: we close the document
        document.close();
        out.close();
    }
    
    
    
}
